package com.school.appleforgetbackend.service;

import com.alibaba.fastjson.JSON;
import com.school.appleforgetbackend.BO.response.WeldResponse;
import com.school.appleforgetbackend.BO.thirdWeld.ThirdWeld;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.*;

@Service
@ConditionalOnProperty(prefix = "mock.service", name = "user", havingValue = "false")
public class RealWeldService implements WeldService {


    @Override
    public WeldResponse getWeld() {
        try {

            RestTemplate restTemplate = new RestTemplate();

            String url1 = "http://172.16.51.80:8082/api/plc";
            HttpHeaders headers = new HttpHeaders();
            headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
            HttpEntity<String> requestHttpEntity = new HttpEntity<>(null, headers);
            ResponseEntity<String> response = restTemplate.exchange(url1, HttpMethod.GET, requestHttpEntity, String.class);
            String originalResult = response.getBody();
            System.out.println("Original Response: " + originalResult);
            WeldResponse weldResponse = new WeldResponse();

            //焊机1
            ThirdWeld thirdWeld = JSON.parseObject(originalResult, ThirdWeld.class);

            weldResponse.setWeld(thirdWeld.getWeld());
            weldResponse.setTimestamp(thirdWeld.getTimestamp());
            weldResponse.setCurrent(thirdWeld.getCurrent());
            weldResponse.setVoltage(thirdWeld.getVoltage());

            System.out.println("Original Response"+"...."+weldResponse);
            return weldResponse;
        } catch (Exception e) {
            WeldResponse weldResponse;
            weldResponse = null;
            System.out.println("接口异常返回"+"...." + weldResponse);
            return weldResponse;
        }
    }

    public WeldResponse getWeld2() {
        try {

            RestTemplate restTemplate = new RestTemplate();
            String url = "http://172.16.51.83:8082/api/plc";
            HttpHeaders headers = new HttpHeaders();
            headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
            headers.set("client", "iOIWB3xWhVmgu91kKIag7LWGCMkhibaInyW3");

            HttpEntity<String> requestHttpEntity = new HttpEntity<>(null, headers);
            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestHttpEntity, String.class);
            String originalResult = response.getBody();
            System.out.println("Original Response: " + originalResult);
            WeldResponse weldResponse = new WeldResponse();
            ThirdWeld thirdWeld = JSON.parseObject(originalResult, ThirdWeld.class);

            //焊机2
            weldResponse.setWeld(thirdWeld.getWeld());
            weldResponse.setTimestamp(thirdWeld.getTimestamp());
            weldResponse.setCurrent(thirdWeld.getCurrent());
            weldResponse.setVoltage(thirdWeld.getVoltage());

            System.out.println("Original Response"+"...."+weldResponse);
            return weldResponse;
        } catch (Exception e) {
            WeldResponse weldResponse;
            weldResponse = null;
            System.out.println("接口异常返回"+"...." + weldResponse);
            return weldResponse;
        }
    }

    public WeldResponse getWeld4() {
        try {

            RestTemplate restTemplate = new RestTemplate();
            String url = "http://172.16.51.86:8082/api/plc";
            HttpHeaders headers = new HttpHeaders();
            headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
            headers.set("client", "iOIWB3xWhVmgu91kKIag7LWGCMkhibaInyW3");

            HttpEntity<String> requestHttpEntity = new HttpEntity<>(null, headers);
            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestHttpEntity, String.class);
            String originalResult = response.getBody();
            System.out.println("Original Response: " + originalResult);
            WeldResponse weldResponse = new WeldResponse();
            ThirdWeld thirdWeld = JSON.parseObject(originalResult, ThirdWeld.class);
            //焊机4

            weldResponse.setWeld(thirdWeld.getWeld());
            weldResponse.setTimestamp(thirdWeld.getTimestamp());
            weldResponse.setCurrent(thirdWeld.getCurrent());
            weldResponse.setVoltage(thirdWeld.getVoltage());

            System.out.println("Original Response"+"...."+weldResponse);
            return weldResponse;
        } catch (Exception e) {
            WeldResponse weldResponse;
            weldResponse = null;
            System.out.println("接口异常返回"+"...." + weldResponse);
            return weldResponse;
        }
    }

    public WeldResponse getWeld5() {
        try {

            RestTemplate restTemplate = new RestTemplate();
            String url = "http://172.16.51.89:8082/api/plc";
            HttpHeaders headers = new HttpHeaders();
            headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
            headers.set("client", "iOIWB3xWhVmgu91kKIag7LWGCMkhibaInyW3");

            HttpEntity<String> requestHttpEntity = new HttpEntity<>(null, headers);
            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestHttpEntity, String.class);
            String originalResult = response.getBody();
            System.out.println("Original Response: " + originalResult);
            WeldResponse weldResponse = new WeldResponse();
            ThirdWeld thirdWeld = JSON.parseObject(originalResult, ThirdWeld.class);
            //焊机5

            weldResponse.setWeld(thirdWeld.getWeld());
            weldResponse.setTimestamp(thirdWeld.getTimestamp());
            weldResponse.setCurrent(thirdWeld.getCurrent());
            weldResponse.setVoltage(thirdWeld.getVoltage());

            System.out.println("Original Response"+"...."+weldResponse);
            return weldResponse;
        } catch (Exception e) {
            WeldResponse weldResponse;
            weldResponse =null;
            System.out.println("接口异常返回"+"...." + weldResponse);
            return weldResponse;
        }
    }
}
